Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 407)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.43607 11.48561 11.53433 11.58226 11.62942 11.67584 11.72153 11.76653
##   [9] 11.81085 11.85452 11.89757 11.94002 11.98190 12.02322 12.06401 12.10425
##  [17] 12.14389 12.18290 12.22124 12.25888 12.29580 12.33196 12.36733 12.40189
##  [25] 12.43559 12.46842 12.50033 12.53169 12.56281 12.59359 12.62393 12.65374
##  [33] 12.68292 12.71136 12.73897 12.76566 12.79132 12.81586 12.83918 12.86117
##  [41] 12.88175 12.90239 12.92423 12.94665 12.96904 12.99078 13.01126 13.02986
##  [49] 13.04597 13.05897 13.06983 13.07986 13.08899 13.09713 13.10420 13.11013
##  [57] 13.11484 13.11826 13.12029 13.12087 13.11992 13.11735 13.11309 13.10706
##  [65] 13.09742 13.08293 13.06436 13.04251 13.01818 12.99215 12.96520 12.93815
##  [73] 12.91176 12.88684 12.86418 12.84456 12.82440 12.80005 12.77218 12.74149
##  [81] 12.70865 12.67435 12.63927 12.60410 12.56952 12.53621 12.50485 12.47614
##  [89] 12.45075 12.42937 12.40926 12.38762 12.36504 12.34213 12.31949 12.29770
##  [97] 12.27737 12.25908 12.24345 12.22985 12.21724 12.20559 12.19486 12.18502
## [105] 12.17604 12.16789 12.16052 12.15392 12.14804 12.14286 12.13833 12.13443
## [113] 12.13112 12.12970 12.13115 12.13499 12.14073 12.14788 12.15596 12.16448
## [121] 12.17296 12.18091 12.18784 12.19326 12.19670 12.19911 12.20175 12.20460
## [129] 12.20763 12.21081 12.21412 12.21754 12.22102 12.22456 12.22812 12.23168
## [137] 12.23521 12.23868 12.24206 12.24443 12.24512 12.24450 12.24294 12.24081
## [145] 12.23850 12.23636 12.23478 12.23413 12.23478 12.23711 12.24148 12.24827
## [153] 12.25786 12.27014 12.28455 12.30080 12.31857 12.33756 12.35748 12.37800
## [161] 12.39883 12.41967 12.44020 12.46013 12.47914 12.49693 12.51321 12.53163
## [169] 12.55544 12.58367 12.61536 12.64954 12.68526 12.72155 12.75746 12.79202
## [177] 12.82427 12.85324 12.87798 12.89752 12.91091 12.92190 12.93462 12.94865
## [185] 12.96357 12.97893 12.99433 13.00934 13.02352 13.03647 13.04774 13.05692
## [193] 13.06358 13.06730 13.06765 13.06421 13.05654 13.04380 13.02586 13.00345
## [201] 12.97729 12.94809 12.91658 12.88348 12.84950 12.81537 12.78179 12.74951
## [209] 12.71922 12.68661 12.64755 12.60305 12.55412 12.50177 12.44703 12.39089
## [217] 12.33437 12.27848 12.22424 12.17265 12.12473 12.08149 12.04395 12.00776
## [225] 11.96862 11.92758 11.88570 11.84402 11.80360 11.76549 11.73073 11.70039
## [233] 11.67209 11.64300 11.61350 11.58396 11.55477 11.52629 11.49891 11.47300
## [241] 11.44893 11.42708 11.40783 11.39155 11.37862 11.36863 11.36082 11.35507
## [249] 11.35122 11.34915 11.34872 11.34979 11.35221 11.35586 11.36059 11.36626
## [257] 11.37275 11.37990 11.39025 11.40591 11.42609 11.45000 11.47688 11.50593
## [265] 11.53636 11.56741 11.59828 11.62819 11.65636 11.68201 11.70435 11.72260
## [273] 11.74063 11.76215 11.78617 11.81171 11.83776 11.86333 11.88743 11.90907
## [281] 11.92724 11.94288 11.95764 11.97167 11.98509 11.99803 12.01062 12.02299
## [289] 12.03527 12.04759 12.06008 12.07287 12.08609 12.09987 12.11434 12.12833
## [297] 12.14077 12.15194 12.16212 12.17159 12.18062 12.18951 12.19852 12.20795
## [305] 12.21806 12.22914 12.24147 12.25500 12.26940 12.28455 12.30034 12.31664
## [313] 12.33333 12.35029 12.36740 12.38453 12.40157 12.41839 12.43488 12.45090
## [321] 12.46635 12.48110 12.49502 12.50925 12.52482 12.54144 12.55881 12.57664
## [329] 12.59466 12.61256 12.63006 12.64687 12.66270 12.67726 12.69026 12.70141
## [337] 12.71040 12.71732 12.72245 12.72611 12.72859 12.73019 12.73122 12.73197
## [345] 12.73275 12.73385 12.73558 12.73824 12.74212 12.74674 12.75138 12.75598
## [353] 12.76046 12.76477 12.76884 12.77261 12.77602 12.77900 12.78161 12.78395
## [361] 12.78604 12.78787 12.78947 12.79084 12.79200 12.79294 12.79368 12.79423
## [369] 12.79461 12.79481 12.79485 12.79474 12.79435 12.79360 12.79253 12.79121
## [377] 12.78967 12.78799 12.78620 12.78437 12.78253 12.78076 12.77910 12.77761
## [385] 12.77617 12.77466 12.77306 12.77138 12.76962 12.76777 12.76582 12.76379
## [393] 12.76166 12.75943 12.75710 12.75467 12.75213 12.74949 12.74676 12.74399
## [401] 12.74116 12.73826 12.73528 12.73223 12.72909 12.72586 12.72253
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 407)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.79447 10.87786 10.95977 11.04016 11.11902 11.19632 11.27205 11.34618
##   [9] 11.41868 11.48955 11.55875 11.62626 11.69207 11.75615 11.81847 11.87906
##  [17] 11.93796 11.99520 12.05081 12.10481 12.15724 12.20814 12.25751 12.30541
##  [25] 12.35184 12.39685 12.44047 12.48238 12.52233 12.56039 12.59664 12.63117
##  [33] 12.66406 12.69539 12.72524 12.75369 12.78082 12.80672 12.83146 12.85514
##  [41] 12.87782 12.89809 12.91487 12.92876 12.94037 12.95029 12.95913 12.96749
##  [49] 12.97597 12.98518 12.99359 12.99947 13.00310 13.00476 13.00473 13.00329
##  [57] 13.00073 12.99731 12.99331 12.98903 12.98474 12.98071 12.97723 12.97458
##  [65] 12.97214 12.96911 12.96547 12.96123 12.95637 12.95090 12.94479 12.93805
##  [73] 12.93066 12.92263 12.91394 12.90458 12.89460 12.88402 12.87285 12.86110
##  [81] 12.84877 12.83586 12.82237 12.80832 12.79370 12.77853 12.76279 12.74651
##  [89] 12.72967 12.71230 12.69401 12.67456 12.65415 12.63294 12.61114 12.58892
##  [97] 12.56647 12.54397 12.52162 12.49634 12.46560 12.43038 12.39166 12.35041
## [105] 12.30760 12.26421 12.22122 12.17960 12.14033 12.10438 12.07272 12.04634
## [113] 12.02621 12.00889 11.99061 11.97179 11.95290 11.93435 11.91661 11.90010
## [121] 11.88528 11.87258 11.86245 11.85533 11.85166 11.85140 11.85403 11.85924
## [129] 11.86672 11.87616 11.88725 11.89969 11.91316 11.92737 11.94199 11.95674
## [137] 11.97129 11.98533 11.99857 12.01354 12.03260 12.05520 12.08076 12.10873
## [145] 12.13854 12.16963 12.20144 12.23340 12.26495 12.29553 12.32457 12.35152
## [153] 12.37580 12.40054 12.42883 12.46006 12.49362 12.52890 12.56529 12.60217
## [161] 12.63893 12.67497 12.70966 12.74241 12.77260 12.79961 12.82284 12.84432
## [169] 12.86635 12.88872 12.91121 12.93362 12.95573 12.97734 12.99823 13.01819
## [177] 13.03700 13.05447 13.07038 13.08451 13.09666 13.10898 13.12342 13.13954
## [185] 13.15686 13.17491 13.19324 13.21137 13.22883 13.24517 13.25991 13.27259
## [193] 13.28274 13.28990 13.29360 13.29336 13.28873 13.28015 13.26856 13.25426
## [201] 13.23753 13.21867 13.19798 13.17574 13.15224 13.12779 13.10267 13.07718
## [209] 13.05161 13.02311 12.98919 12.95063 12.90823 12.86277 12.81505 12.76586
## [217] 12.71599 12.66623 12.61737 12.57020 12.52551 12.48410 12.44675 12.40907
## [225] 12.36697 12.32173 12.27465 12.22699 12.18003 12.13507 12.09337 12.05622
## [233] 12.02011 11.98114 11.93997 11.89727 11.85373 11.81002 11.76681 11.72477
## [241] 11.68457 11.64690 11.61242 11.58181 11.55575 11.53248 11.50990 11.48818
## [249] 11.46747 11.44791 11.42967 11.41290 11.39775 11.38437 11.37293 11.36356
## [257] 11.35644 11.35170 11.35026 11.35264 11.35838 11.36704 11.37818 11.39135
## [265] 11.40611 11.42200 11.43859 11.45543 11.47207 11.48808 11.50299 11.51638
## [273] 11.53056 11.54774 11.56722 11.58835 11.61044 11.63283 11.65485 11.67581
## [281] 11.69506 11.71476 11.73726 11.76216 11.78904 11.81749 11.84709 11.87744
## [289] 11.90811 11.93871 11.96880 11.99799 12.02586 12.05199 12.07597 12.09913
## [297] 12.12293 12.14720 12.17174 12.19637 12.22090 12.24516 12.26895 12.29210
## [305] 12.31441 12.33570 12.35579 12.37508 12.39409 12.41281 12.43124 12.44937
## [313] 12.46720 12.48472 12.50193 12.51882 12.53538 12.55161 12.56750 12.58305
## [321] 12.59825 12.61310 12.62758 12.64190 12.65619 12.67043 12.68455 12.69851
## [329] 12.71226 12.72575 12.73893 12.75176 12.76419 12.77616 12.78763 12.79856
## [337] 12.80815 12.81584 12.82194 12.82673 12.83052 12.83359 12.83625 12.83879
## [345] 12.84151 12.84471 12.84867 12.85369 12.86008 12.86754 12.87551 12.88382
## [353] 12.89231 12.90083 12.90922 12.91732 12.92497 12.93202 12.93891 12.94615
## [361] 12.95364 12.96131 12.96905 12.97680 12.98446 12.99195 12.99918 13.00606
## [369] 13.01251 13.01845 13.02378 13.02843 13.03264 13.03674 13.04069 13.04448
## [377] 13.04809 13.05150 13.05469 13.05765 13.06036 13.06280 13.06494 13.06678
## [385] 13.06836 13.06974 13.07092 13.07188 13.07264 13.07317 13.07347 13.07353
## [393] 13.07336 13.07294 13.07226 13.07132 13.07012 13.06865 13.06688 13.06480
## [401] 13.06244 13.05979 13.05688 13.05371 13.05030 13.04665 13.04279
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 407)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.85989 10.91969 10.97847 11.03625 11.09300 11.14872 11.20342 11.25708
##   [9] 11.30969 11.36126 11.41177 11.46122 11.50960 11.55692 11.60315 11.64835
##  [17] 11.69256 11.73578 11.77798 11.81917 11.85935 11.89849 11.93660 11.97367
##  [25] 12.00969 12.04465 12.07855 12.11125 12.14267 12.17285 12.20184 12.22967
##  [33] 12.25640 12.28206 12.30672 12.33040 12.35316 12.37503 12.39608 12.41633
##  [41] 12.43584 12.45456 12.47240 12.48937 12.50547 12.52069 12.53505 12.54853
##  [49] 12.56114 12.57287 12.58381 12.59401 12.60345 12.61211 12.61994 12.62694
##  [57] 12.63307 12.63831 12.64263 12.64600 12.64841 12.64982 12.65021 12.64955
##  [65] 12.64711 12.64236 12.63561 12.62715 12.61727 12.60627 12.59444 12.58208
##  [73] 12.56948 12.55695 12.54476 12.53323 12.52020 12.50368 12.48424 12.46240
##  [81] 12.43872 12.41374 12.38800 12.36206 12.33646 12.31174 12.28844 12.26713
##  [89] 12.24832 12.23259 12.21767 12.20129 12.18394 12.16610 12.14825 12.13090
##  [97] 12.11451 12.09958 12.08661 12.07511 12.06425 12.05398 12.04426 12.03503
## [105] 12.02623 12.01781 12.00973 12.00192 11.99434 11.98693 11.97965 11.97243
## [113] 11.96523 11.95878 11.95369 11.94974 11.94672 11.94440 11.94255 11.94097
## [121] 11.93942 11.93768 11.93554 11.93276 11.92914 11.92469 11.91969 11.91425
## [129] 11.90851 11.90260 11.89665 11.89079 11.88515 11.87986 11.87505 11.87085
## [137] 11.86740 11.86482 11.86324 11.86073 11.85565 11.84854 11.83996 11.83047
## [145] 11.82062 11.81095 11.80203 11.79441 11.78864 11.78528 11.78488 11.78799
## [153] 11.79516 11.80589 11.81909 11.83446 11.85173 11.87058 11.89074 11.91191
## [161] 11.93379 11.95609 11.97852 12.00079 12.02260 12.04367 12.06369 12.08650
## [169] 12.11541 12.14942 12.18751 12.22865 12.27183 12.31602 12.36021 12.40338
## [177] 12.44451 12.48258 12.51656 12.54545 12.56821 12.58955 12.61439 12.64205
## [185] 12.67189 12.70321 12.73537 12.76769 12.79949 12.83013 12.85892 12.88519
## [193] 12.90829 12.92754 12.94228 12.95183 12.95553 12.95378 12.94777 12.93799
## [201] 12.92496 12.90920 12.89119 12.87147 12.85053 12.82888 12.80703 12.78549
## [209] 12.76477 12.74073 12.70964 12.67256 12.63059 12.58482 12.53632 12.48619
## [217] 12.43550 12.38535 12.33681 12.29098 12.24894 12.21177 12.18056 12.14918
## [225] 12.11200 12.07083 12.02749 11.98382 11.94162 11.90273 11.86896 11.84214
## [233] 11.82004 11.79916 11.77946 11.76088 11.74335 11.72683 11.71125 11.69655
## [241] 11.68269 11.66959 11.65721 11.64548 11.63435 11.62521 11.61919 11.61587
## [249] 11.61481 11.61560 11.61779 11.62096 11.62469 11.62854 11.63209 11.63490
## [257] 11.63655 11.63660 11.63603 11.63607 11.63672 11.63799 11.63988 11.64240
## [265] 11.64553 11.64930 11.65369 11.65872 11.66437 11.67067 11.67760 11.68517
## [273] 11.69337 11.70211 11.71131 11.72089 11.73077 11.74086 11.75107 11.76132
## [281] 11.77154 11.78233 11.79427 11.80720 11.82095 11.83536 11.85026 11.86550
## [289] 11.88091 11.89632 11.91158 11.92651 11.94097 11.95477 11.96777 11.98167
## [297] 11.99801 12.01632 12.03614 12.05701 12.07846 12.10004 12.12129 12.14174
## [305] 12.16093 12.17841 12.19370 12.20757 12.22109 12.23422 12.24697 12.25933
## [313] 12.27126 12.28278 12.29385 12.30448 12.31464 12.32432 12.33351 12.34220
## [321] 12.35038 12.35803 12.36513 12.37168 12.37768 12.38319 12.38825 12.39292
## [329] 12.39724 12.40125 12.40502 12.40857 12.41197 12.41525 12.41848 12.42169
## [337] 12.42495 12.42825 12.43155 12.43481 12.43798 12.44101 12.44387 12.44650
## [345] 12.44886 12.45090 12.45259 12.45387 12.45470 12.45370 12.45002 12.44438
## [353] 12.43749 12.43009 12.42288 12.41659 12.41194 12.40965 12.40847 12.40673
## [361] 12.40455 12.40204 12.39931 12.39648 12.39366 12.39096 12.38850 12.38638
## [369] 12.38472 12.38364 12.38325 12.38365 12.38455 12.38557 12.38673 12.38805
## [377] 12.38955 12.39123 12.39312 12.39524 12.39759 12.40021 12.40309 12.40627
## [385] 12.40970 12.41335 12.41721 12.42129 12.42558 12.43009 12.43481 12.43975
## [393] 12.44491 12.45028 12.45587 12.46168 12.46770 12.47395 12.48041 12.48708
## [401] 12.49397 12.50107 12.50839 12.51591 12.52364 12.53159 12.53974
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")